package com.sun.myproject.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ImgServlet
 */
@WebServlet("/ImgServlet")
public class ImgServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String filename = request.getParameter("pic");
		System.out.println(this.getServletContext().getRealPath("/BookImg/"+filename));
		
		InputStream in = new FileInputStream(this.getServletContext().getRealPath("/BookImg/"+filename));
		OutputStream out = response.getOutputStream();
		byte[] buffer = new byte[1024];
		while (in.read(buffer) != -1) {
			out.write(buffer);
		}
		out.flush();
		in.close();
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}


}
